//
//  CityManager.m
//  动起来
//
//  Created by 美鑫科技 on 16/8/10.
//  Copyright © 2016年 MorpLCP. All rights reserved.
//

#import "CityManager.h"
#import "pinyin.h"

@interface CityManager ()

@property (nonatomic, strong) NSMutableDictionary *dataDict;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *keyArray;

@end

@implementation CityManager

- (instancetype)init {
    self = [super init];
    if (self) {
        self.dataDict = [NSMutableDictionary dictionary];
        self.dataArray = [NSMutableArray array];
        [self analyticalData];
    }
    return self;
}

+ (instancetype)manager {
    static CityManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [CityManager new];
    });
    return manager;
}

// 解析数据
- (void)analyticalData {
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"City" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSArray *prArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    self.dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSMutableArray *allCityArray = [NSMutableArray array];
    for (NSDictionary *pr in prArray) {
        NSArray *cityArray = pr[@"city"];
        [allCityArray addObjectsFromArray:cityArray];
    }
    
    for (NSDictionary *dict in allCityArray) {
        if (self.dataDict[[dict[@"name"] uppercasePinYinFirstLetter]] == nil) {
            NSMutableArray *array = [NSMutableArray arrayWithObject:dict];
            [self.dataDict setObject:array forKey:[dict[@"name"] uppercasePinYinFirstLetter]];
        } else {
            NSMutableArray *array = self.dataDict[[dict[@"name"] uppercasePinYinFirstLetter]];
            [array addObject:dict];
            [self.dataDict setObject:array forKey:[dict[@"name"] uppercasePinYinFirstLetter]];
        }
    }
    
    self.keyArray = [NSMutableArray arrayWithArray:self.dataDict.allKeys];
    [self.keyArray sortUsingSelector:@selector(compare:)];
    
}

- (NSArray *)getDataArray {
    return self.dataArray;
}

- (NSArray *)getKeyArray {
    return self.keyArray;
}

- (NSDictionary *)getDataDict {
    return self.dataDict;
}

- (NSArray *)getAreaWithCityName:(NSString *)cityName {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"City" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSArray *prArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSMutableArray *areaArray = [NSMutableArray array];
    for (NSDictionary *pr in prArray) {
        NSArray *cityArray = pr[@"city"];
        for (NSDictionary *city in cityArray) {
            if ([city[@"name"] isEqualToString:cityName]) {
                [areaArray addObjectsFromArray:city[@"area"]];
                break;
            }
        }
    }
    return areaArray;
}

- (NSArray *)getHotCity {
    return @[@"北京", @"上海", @"广州", @"深圳", @"杭州", @"南京", @"天津", @"武汉", @"重庆"];
}

@end
